<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>

</head>

<body>
    <p>人的朋友的女朋友的闺蜜的姓名：</p>
    <p class="p1"></p>
    <p class="p2"></p>
    <p class="p3"></p>
    <script>
        let people = {
            name: "奉先",
            friend: {
                name: "罗佰君",
                girlFriend: {
                    name: "胡正",
                    harem: {
                        name: "王魏辉"
                    }
                }
            }
        }

        let p1 = document.querySelector(".p1");
        let friend = people.friend;
        if (friend != null) {
            let girlFriend = friend.girlFriend;
            if (girlFriend != null) {
                let harem = girlFriend.harem;
                if (harem != null) {
                    p1.innerText = harem.name;
                }
            }
        }

        let p2 = document.querySelector(".p2");
        const name = (people
            && people.friend
            && people.friend.girlFriend
            && people.friend.girlFriend.harem
            && people.friend.girlFriend.harem.name) || 'default';
        p2.innerText = name;

        let p3 = document.querySelector(".p3");
        // ES6的链式判断可以简写：
        const name2 = people?.friend?.girlFriend?.harem?.name || 'default';
        p3.innerText = name2;
    </script>
</body>

</html>